/**
 * Copyright (c) 2021 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.api;

import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.modules.stat.dto.StatChannelDTO;
import io.renren.modules.stat.enums.ChannelStatusEnum;
import io.renren.modules.stat.service.StatChannelConfService;
import io.renren.modules.stat.service.StatChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 前端API
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("api/stat")
@Api(tags = "前端API")
public class ApiStatController {
    @Autowired
    private StatChannelService statChannelService;
    @Autowired
    private StatChannelConfService statChannelConfService;

    @GetMapping("channelList")
    @ApiOperation("列表")
    public Result<List<StatChannelDTO>> channelList() {
        Map<String, Object> params = new HashMap<>();
        params.put("status", String.valueOf(ChannelStatusEnum.ENABLED.value()));
        List<StatChannelDTO> data = statChannelService.list(params);
        return new Result<List<StatChannelDTO>>().ok(data);
    }

    @GetMapping("channel/{id}")
    @ApiOperation("列表")
    public Result<StatChannelDTO> channelList(@PathVariable("id") Long id) {
        StatChannelDTO data = statChannelService.get(id);
        data.setConf(statChannelConfService.getByChannel(data.getId()).getConf());
        return new Result<StatChannelDTO>().ok(data);
    }

    @GetMapping("data/columns")
    @ApiOperation("标题列表")
    public Result<List<String>> columns(@ApiIgnore @RequestParam Map<String, Object> params) {
        List<String> columns = statChannelService.columns(params);
        return new Result<List<String>>().ok(columns);
    }

    @GetMapping("data/page")
    @ApiOperation("分页")
    public PageData<Map<String, String>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<Map<String, String>> page = statChannelService.dataPage(params);
        return page;
    }

}